//
// Created by xqls on 12-8-28.
//
// To change the template use AppCode | Preferences | File Templates.
//


#import "MessageSendPanel.h"


@implementation CMessageSendPanel

@synthesize m_delegate;

@synthesize m_textView;

@synthesize m_doneBtn;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    self.backgroundColor=[UIColor grayColor];
    
    m_textView = [[HPGrowingTextView alloc] initWithFrame:CGRectMake(5, 3, 260, 0)];
    m_textView.contentInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
	m_textView.minNumberOfLines = 1;
	m_textView.maxNumberOfLines = 3;
	m_textView.returnKeyType = UIReturnKeyDefault; //just as an example
	m_textView.font = [UIFont systemFontOfSize:15.0f];
	m_textView.delegate = self;
    m_textView.internalTextView.scrollIndicatorInsets = UIEdgeInsetsMake(5, 0, 5, 0);
    m_textView.backgroundColor = [UIColor whiteColor];
    CGRect f=m_textView.frame;
    f.origin.y=(self.height-f.size.height)/2;
    m_textView.frame=f;
    [self addSubview:m_textView];
    
    UIImage *rawEntryBackground = [UIImage imageNamed:@"MessageEntryInputField.png"];
    UIImage *entryBackground = [rawEntryBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    UIImageView *entryImageView = [[[UIImageView alloc] initWithImage:entryBackground] autorelease];
    entryImageView.frame = CGRectMake(m_textView.left, 0, m_textView.width, self.height);
    entryImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    UIImage *rawBackground = [UIImage imageNamed:@"MessageEntryBackground.png"];
    UIImage *background = [rawBackground stretchableImageWithLeftCapWidth:13 topCapHeight:22];
    UIImageView *imageView = [[[UIImageView alloc] initWithImage:background] autorelease];
    imageView.frame = CGRectMake(0, 0, self.width, self.height);
    imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    m_textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    
    [self addSubview:imageView];
    [self addSubview:m_textView];
    [self addSubview:entryImageView];
    
    m_doneBtn = [[UIButton buttonWithType:UIButtonTypeCustom]retain];
	m_doneBtn.frame = CGRectMake(self.width - 55, 8, 50, 30);
    m_doneBtn.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
//	[m_doneBtn setTitle:@"Done" forState:UIControlStateNormal];
    
    [m_doneBtn setTitleShadowColor:[UIColor colorWithWhite:0 alpha:0.4] forState:UIControlStateNormal];
    m_doneBtn.titleLabel.shadowOffset = CGSizeMake (0.0, -1.0);
    m_doneBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    
    [m_doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[m_doneBtn addTarget:self action:@selector(DoneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [m_doneBtn setBackgroundImage:[UIImage imageNamed:@"MessageEntrySendButton.png"] forState:UIControlStateNormal];
	[self addSubview:m_doneBtn];
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    
    return self;
}

-(void)DoneBtnClick:(UIButton*)btn
{
    if([m_delegate respondsToSelector:@selector(MessagePanelSendText:text:)]==YES)
        [m_delegate MessagePanelSendText:self text:m_textView.text];
}

- (BOOL)growingTextViewShouldBeginEditing:(HPGrowingTextView *)growingTextView
{
    if([m_delegate respondsToSelector:@selector(MessagePanelShouldBeginEdit:)]==YES)
         return [m_delegate MessagePanelShouldBeginEdit:self];
    
    return YES;
}

- (BOOL)growingTextViewShouldEndEditing:(HPGrowingTextView *)growingTextView
{
    if([m_delegate respondsToSelector:@selector(MessagePanelShouldEndEdit:)]==YES)
        return [m_delegate MessagePanelShouldEndEdit:self];
    
    return YES;
}

- (void)growingTextViewDidBeginEditing:(HPGrowingTextView *)growingTextView
{
    if([m_delegate respondsToSelector:@selector(MessagePanelDidBeginEdit:)]==YES)
        [m_delegate MessagePanelDidBeginEdit:self];
}

- (void)growingTextViewDidEndEditing:(HPGrowingTextView *)growingTextView
{
    if([m_delegate respondsToSelector:@selector(MessagePanelDidEndEdit:)]==YES)
        [m_delegate MessagePanelDidEndEdit:self];
}

- (BOOL)growingTextView:(HPGrowingTextView *)growingTextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}

- (void)growingTextViewDidChange:(HPGrowingTextView *)growingTextView
{
    if([m_delegate respondsToSelector:@selector(MessagePanelTextDidChanged:)]==YES)
        [m_delegate MessagePanelTextDidChanged:self];
}

- (void)growingTextView:(HPGrowingTextView *)growingTextView willChangeHeight:(CGFloat)height
{
    CGFloat diff = (m_textView.frame.size.height - height);
	CGRect r = self.frame;
    r.size.height -= diff;
    r.origin.y += diff;
	self.frame = r;
    
    if([m_delegate respondsToSelector:@selector(MessagePanelFrameWillChanged:height:)]==YES)
        [m_delegate MessagePanelFrameWillChanged:self height:height];
}

- (void)growingTextView:(HPGrowingTextView *)growingTextView didChangeHeight:(CGFloat)height
{
    if([m_delegate respondsToSelector:@selector(MessagePanelFrameDidChanged:height:)]==YES)
        [m_delegate MessagePanelFrameDidChanged:self height:height];
}

- (void)growingTextViewDidChangeSelection:(HPGrowingTextView *)growingTextView
{
    
}

- (BOOL)growingTextViewShouldReturn:(HPGrowingTextView *)growingTextView
{
    return YES;
}

-(void)dealloc
{
    [m_textView release];
    [m_doneBtn release];
    [super dealloc];
}

@end